package cn.vcjf.weapp.api.user.service.impl;

import cn.vcjf.weapp.api.goods.entity.GoodsOrder;
import cn.vcjf.weapp.api.goods.entity.GoodsOrderItem;
import cn.vcjf.weapp.api.goods.mapper.GoodsOrderMapper;
import cn.vcjf.weapp.api.setting.entity.OrderOptionConfig;
import cn.vcjf.weapp.api.user.entity.UserComment;
import cn.vcjf.weapp.api.user.mapper.UserCommentMapper;
import cn.vcjf.weapp.api.user.service.UserCommentService;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhengzihang
 * @since 2018-09-25
 */
@Service("userCommentService")
public class UserCommentServiceImpl extends ServiceImpl<UserCommentMapper, UserComment> implements UserCommentService {

	private Logger logger = LogManager.getLogger(UserCommentServiceImpl.class);

	@Resource
	private GoodsOrderMapper goodsOrderMapper;
	@Resource
	private OrderOptionConfig orderOptionConfig;

	@Scheduled(cron = "0 0 17 * * ?")//每17都执行
	@Override
	public void autoAddUserComment() {
		List<UserComment> userCommentList = new ArrayList<>();
		logger.info("*********自动进行订单商品5星好评*****************开始");
		List<GoodsOrder> goodsOrders = goodsOrderMapper.selectNotCommentOrderList(orderOptionConfig.NotCommentOrderExpireDays);

		for (GoodsOrder order : goodsOrders) {
			List<GoodsOrderItem> orderItems = order.getOrderItems();
			//拼团订单的自动评价
			if(order.getOrderType() == 2 || order.getOrderType() == 3){
				UserComment userComment = new UserComment();
				userComment.setUserId(order.getUserId());
				userComment.setOrderId(order.getId());
				userComment.setCommentText("");
				userComment.setDegreeLevel(5);

				userComment.setGoodsId(order.getGoodsId());
				userCommentList.add(userComment);
				logger.info("*********自动进行订单商品5星好评*****************拼团,砍价订单:{}", JSON.toJSONString(userComment));
			//普通订单和套餐的自动评价
			}else {
				for (GoodsOrderItem item : orderItems) {
					UserComment userComment = new UserComment();
					userComment.setUserId(order.getUserId());
					userComment.setOrderId(order.getId());
					userComment.setCommentText("");
					userComment.setDegreeLevel(5);

					userComment.setGoodsId(item.getGoodsId());
					userCommentList.add(userComment);
					logger.info("*********自动进行订单商品5星好评*****************普通订单和套餐订单:{}", JSON.toJSONString(userComment));
				}
			}

		}
		this.insertBatch(userCommentList);
		logger.info("*********自动进行订单商品5星好评*****************结束");
	}
}
